#include "vgdb.h"

void vcmd_init()
{
    vcmd_func(vcmd_vcid("quit"     )) = vcmd_quit;
    vcmd_func(vcmd_vcid("print"    )) = vcmd_print;
    vcmd_func(vcmd_vcid("step"     )) = vcmd_step;
    vcmd_func(vcmd_vcid("run"      )) = vcmd_run;
    vcmd_func(vcmd_vcid("break"    )) = vcmd_break;
    vcmd_func(vcmd_vcid("continue" )) = vcmd_continue;
    vcmd_func(vcmd_vcid("clear"    )) = vcmd_clear;
    vcmd_func(vcmd_vcid("initopwin")) = vcmd_initopwin;
}


int vcmd_hash(const char* str, int start, int end)
{
    int  p, ret, key;
    char ch;

    if((start < 0) || (end < 0) || (start>end)){
        return end+1;
    }else{
        ret = 0;
        p   = 0;
        key = 77;
        while(str[p]){
            ch  = str[p]-'a';
            //ret += (ret<<(ch%sizeof(ret))^ch*(key<<p%sizeof(key))+ch*key*p);
            ret += ch*key;
            p++;
        }
        return ret%(end-start+1) + start;
    }
}


void vcmd_quit(vcmd_t *vcmd)
{
    gdb_exec("quit\n");
    waitpid(gdb()->pid, NULL, 0);
    gdb()->state = VGDB_GS_QUIT;
}

void vcmd_step(vcmd_t *vcmd)
{
    gdb_exec("step\n");
    gdb()->state = VGDB_GS_QUIT;
}

void vcmd_run(vcmd_t *vcmd)
{
    char    cmd[128];

    sprintf(cmd, "run %s\n", vcmd->args);
    gdb_exec(cmd);
    gdb()->state = VGDB_GS_RUN;
}

void vcmd_break(vcmd_t *vcmd)
{
    char    cmd[128];
    sprintf(cmd, "break %s\n", vcmd->args);
    gdb_exec(cmd);
}

void vcmd_continue(vcmd_t *vcmd)
{
    gdb_exec("continue\n");
}

void vcmd_clear(vcmd_t *vcmd)
{
    char    cmd[128];
    sprintf(cmd, "clear %s\n", vcmd->args);
    gdb_exec(cmd);
}


void vcmd_initopwin(vcmd_t *vcmd)
{
    vmsg_send("%d:create!%d\n", VGDB_BID_GOUTP, vim()->seqno);
    vmsg_send("%d:startDocumentListen!%d\n", VGDB_BID_GOUTP, vim()->seqno);
    vmsg_send("%d:setTitle!%d \"gdb_outp\"\n", VGDB_BID_GOUTP, vim()->seqno);
    vmsg_send("%d:setFullName!%d \"gdb_outp\"\n", VGDB_BID_GOUTP, vim()->seqno);
    vmsg_send("%d:initDone!%d\n", VGDB_BID_GOUTP, vim()->seqno);
    vmsg_send("%d:insert/%d 1 \"gdb output window init sucessfully\\n\"\n", VGDB_BID_GOUTP, vim()->seqno);
}



void vcmd_print(vcmd_t *vcmd)
{
    char    cmd[128];
    sprintf(cmd, "print %s\n", vcmd->args);
    gdb_exec(cmd);
}
